package com.htx.zqs.blesmartmask.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by KomoriWu
 * on 2019/2/23.
 */

public class MySpUtils {
    public static final String KEY_FIRST_DEVICE = "key_first_device";//第一个连接的设备
    public static final String KEY_VOLTAGE_LOW = "key_voltage_low";//当前电压进度
    public static final String KEY_VOLTAGE_MIDDLE = "key_voltage_middle";//当前电压进度
    public static final String KEY_VOLTAGE_STRONG = "key_voltage_strong";//当前电压进度
    public static final String KEY_VOLTAGE_MAP = "key_voltage_map";//当前电压进度
    public static final String KEY_VOLTAGE_POS = "key_voltage_pos";//当前电压高中低索引
    public static final String KEY_BEFORE_COURSE = "key_before_course";//做课程之前要上传的map
    public static final String KEY_AFTER_COURSE = "key_after_course";//做课程之后要上传的map
    public static final String KEY_DEVICE_USE_TIME = "key_device_use_time";//设备使用时间
    public static final String KEY_DEVICE_USE_NUM = "key_device_use_num";//设备使用次数
    public static final String KEY_STAR_AUTO_INCREMENT = "key_star_auto_increment";//开始累计设备使用次数时间==开始课程
    public static final String KEY_USER_NAME = "key_user_name";//用户名字
    public static final String KEY_COURSE_VOLTAGE = "key_course_voltage";//电压
    public static final String KEY_COURSE_TIME = "key_course_time";//课程时间
    public static final String KEY_END_COURSE_TEST = "key_end_course_test";//课程结束需要测试油水

    private static SharedPreferences sp;
    private static SharedPreferences.Editor editor;
    private static MySpUtils sMySpUtils;


    /**
     * 初始化SharedPreferencesUtil,只需要初始化一次，建议在Application中初始化
     *
     * @param context 上下文对象
     * @param name    SharedPreferences Name
     */
    public static void getInstance(Context context, String name) {
        if (sMySpUtils == null) {
            sMySpUtils = new MySpUtils(context, name);
        }
    }

    @SuppressLint("CommitPrefEdits")
    private MySpUtils(Context context, String name) {
        if (sp == null) {
            sp = context.getSharedPreferences(name, Context.MODE_MULTI_PROCESS);
            editor = sp.edit();
        }
    }

    /**
     * 存入字符串
     *
     * @param key   字符串的键
     * @param value 字符串的值
     */
    public static void putString(String key, String value) {
        editor.putString(key, value);
        editor.commit();
    }

    /**
     * 获取字符串
     *
     * @param key 字符串的键
     * @return 得到的字符串
     */
    public static String getString(String key) {
        return sp.getString(key, "");
    }

    /**
     * 获取字符串
     *
     * @param key   字符串的键
     * @param value 字符串的默认值
     * @return 得到的字符串
     */
    public static String getString(String key, String value) {
        return sp.getString(key, value);
    }

    /**
     * 保存布尔值
     *
     * @param key   键
     * @param value 值
     */
    public static void putBoolean(String key, boolean value) {
        editor.putBoolean(key, value);
        editor.commit();
    }

    /**
     * 获取布尔值
     *
     * @param key      键
     * @param defValue 默认值
     * @return 返回保存的值
     */
    public static boolean getBoolean(String key, boolean defValue) {
        return sp.getBoolean(key, defValue);
    }

    /**
     * 保存int值
     *
     * @param key   键
     * @param value 值
     */
    public static void putInt(String key, int value) {
        editor.putInt(key, value);
        editor.commit();
    }

    /**
     * 获取int值
     *
     * @param key      键
     * @param defValue 默认值
     * @return 保存的值
     */
    public static int getInt(String key, int defValue) {
        return sp.getInt(key, defValue);
    }

    /**
     * 用于保存Map集合
     *
     * @param key key
     * @param map map数据
     * @return 保存结果
     */

    public static <K, T> void putHashMapData(String key, Map<K, T> map) {
        if (map == null || map.isEmpty()) {
            return;
        }

        Gson gson = new Gson();
        String strJson = gson.toJson(map);
        editor.putString(key, strJson);
        editor.commit();
    }


    /**
     * 获取Map集合
     */
    public static <K, T> HashMap<K, T> getHashMapData(String key) {
        HashMap<K, T> map = new HashMap<>();
        String strJson = sp.getString(key, null);
        if (TextUtils.isEmpty(strJson)) {
            return map;
        }
        Gson gson = new Gson();
        map = gson.fromJson(strJson, new TypeToken<Map<K, T>>() {
        }.getType());
        return map;
    }

}
